package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Comment;
import com.qy.model.PolicyOrder;
import com.qy.service.CommentService;
import com.qy.service.PolicyOrderService;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-CommentController")
@RequestMapping("/manage/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private PolicyOrderService policyOrderService;

    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        commentService.save(comment);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        commentService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Comment comment) {
        commentService.update(comment);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Comment comment = commentService.findById(id);
        return ResultGenerator.successResult(comment);
    }

    @GetMapping("/list")
    public Result list(PageBean<Comment> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Comment> list = commentService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //评价列表-标签获取
    @GetMapping("/commentLabel")
    public Result commentLabel(){
        List<Map> label = commentService.commentLabel();
        return ResultGenerator.successResult(label);
    }

    //评价列表-查询
    @GetMapping("commentList")
    public Result commentList(PageBean<Map> page,Integer type ,String content ,String label){
        Comment comment = new Comment();
        if (type == 1){
            comment.setOrder_number(content);
        }else if (type == 2){
            comment.setName(content);
        }else if (type == 3){
            comment.setPhone_number(content);
        }
        comment.setLabel(label);
        PageHelper.startPage(page.getPageNum(), page.getSize() , "add_time desc");
        List<Map> commentList = commentService.commentList(comment);
        page.setList(commentList);
        return ResultGenerator.successResult(page);
    }

    //评价列表-查看订单
    @GetMapping("/seePolicyOrder")
    public Result seePolicyOrder(Integer id) {
        if (id == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Map> list = policyOrderService.seePolicyOrder(id);
        return ResultGenerator.successResult(list);
    }

    //评价列表-删除
    @PostMapping("/deleteComment")
    public Result deleteComment(@RequestBody Comment comment){
        if (comment.getId() == null || comment.getId() == 0){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        commentService.deleteById(comment.getId());
        return ResultGenerator.successResult();
    }

}
